package com.hxzy.framework.captcha.service;

import com.xingyuv.captcha.service.CaptchaCacheService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author wy
 * @Description 图片滑块验证码的实现
 * @Date 2024/1/23 11:31
 * @Version 1.0
 */
@NoArgsConstructor
@AllArgsConstructor
public class RedisCaptchaServiceImpl  implements CaptchaCacheService {

    /**
     * 保证 aj-captcha 的 SPI 创建时的注入
     */
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String type() {
        return "redis";
    }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }



    @Override
    public void set(String key, String value, long expiresInSeconds) {
        this.stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS);
    }

    @Override
    public boolean exists(String key) {
        return Boolean.TRUE.equals(this.stringRedisTemplate.hasKey(key));
    }

    @Override
    public void delete(String key) {
        this.stringRedisTemplate.delete(key);
    }

    @Override
    public String get(String key) {
        return this.stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 验证输入错误次数
     * @param key
     * @param val
     * @return
     */
    @Override
    public Long increment(String key, long val) {
        return this.stringRedisTemplate.opsForValue().increment(key,val);
    }


}
